package com.fit.dao.certificate;

import java.util.List;

import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.certificate.Certificate;
import com.fit.entities.user.User;

public class CertificateDAOImpl implements CertificateDAO {

	@Inject
	private Session session;

	@Log
	public boolean save(Certificate newCertificate) {
		if (newCertificate != null) {
			session.save(newCertificate);
			return true;
		}
		return false;
	}

	@Log
	public boolean delete(Certificate certificate) {
		if (certificate != null) {
			session.delete(certificate);
			return true;
		}
		return false;
	}

	@Log
	public boolean update(Certificate certificate) {
		if (certificate != null) {
			session.update(certificate);
			return true;
		}
		return false;
	}

	@Log
	public List findAll() {
		return session.createCriteria(Certificate.class).list();
	}

	@Log
	public boolean deleteByID(Long id) {
		Certificate u = findByID(id);
		if (u != null) {
			delete(u);
			return true;
		}
		return false;
	}

	@Log
	public Certificate findByID(Long id) {
		if (id != null) {
			return (Certificate) session.get(Certificate.class, id);
		}
		return null;
	}

	@Log
	public Certificate findByCertificateName(String name) {
		if (name == null ) {
			return null;
		}

		if (name.equals("") ) {
			return null;
		}

		List l = session.createCriteria(Certificate.class).add(Restrictions.eq("name", name)).list();
		Certificate c = null;
		if (l != null) {
			if (l.size() > 0) {
				c = (Certificate) l.get(0);
			}
		}
		return c;
	}

}
